﻿<?php 
class Model_Category extends Zend_Db_Table_Abstract
{
	protected $_name = 'category';
	public function createCategory ($cat_ten,$cat_parentID,$cat_mota,$orderby)
	{
			$rowCat = $this->createRow();
			if ($rowCat) {
				// update the row values
				$rowCat->cat_ten = $cat_ten;
				$rowCat->cat_parentID = $cat_parentID;
				$rowCat->cat_mota = $cat_mota;
				$rowCat->orderby = $orderby;				
				$rowCat->save();
				//return the new cat
				return $rowCat;
			} else {
				throw new Zend_Exception("Không thể tạo category mới !!! ");
			}
	}
	
	
	public static function getAllCategory()
	{
		$catModel = new self();
		$select = $catModel->select();
		$select->order(array('cat_id'));	
		return $catModel->fetchAll($select);
	}		
	
	public static function getParentCategory()
	{
		$catModel = new self();
		$select = $catModel->select();
		$select->order(array('cat_id'))
		       ->where('cat_parentID =?',0);	
		return $catModel->fetchAll($select);
	}	

	public static function getCategoryNotRoot()
	{
		$catModel = new self();
		$select = $catModel->select();
		$select->order(array('cat_id'))
		       ->where('cat_parentID !=?',0);	
		return $catModel->fetchAll($select);
	}		
	
	public static function getChildCategory($cat_parentID)
	{
		$catModel = new self();
		$select = $catModel->select();
		$select->order(array('cat_id'))
		       ->where('cat_parentID =?',$cat_parentID);	
		return $catModel->fetchAll($select);
	}
	
	public static function getCategoryByID($cat_id)
	{
		$catModel = new self();
		$select = $catModel->select();
		$select->order(array('cat_id'))
		       ->where('cat_id =?',$cat_id);	
		return $catModel->fetchAll($select);
	
	}
	
	
	// Đứa nào viết không biết, không hiểu là viết cái gì ???
	// LongTD
    public function getCatnamebycatID($ar)
		{
		  $catModel = new self();
		  $select = $catModel->select();
		  $select->distinct()
		       ->from(array('c' => 'category'),
                      array('cat_id','cat_ten'))
				->where('cat_id IN (?)',$ar);
		return $catModel->fetchAll($select);
		}
	
	
	
	
}